//package medicalApp.velociraptors.com;

import java.awt.CardLayout;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JPanel;

/**
 * Controls the entire program. Allows each panel to talk to each other and controls the updating of the dynamic panel
 * @author Vishnu
 *
 */
@SuppressWarnings("serial")
public class MedAppController extends JPanel {
	private LoginGUI login;
	private CardLayout cl = new CardLayout();
	@SuppressWarnings("unused")
	private MedAppGUI frame;
	private boolean loginSuccessful;
	private String currentCard;
	private String currentUser;
	private LoginSuccessful loginSuccess;
	@SuppressWarnings("unused")
	private String accountType;
	private DoctorPanel dP;
	private PatientPanel patientPanel;
	private NursePanel nursePanel;
	private SysAdminPanel saPanel;
	private Connection con;
	private int doctorsOrderID;
	private int treatmentRecordID;
	public PrintWriter out;
	/**
	 * Constructor for the MedAppController class
	 * @param frame	The JFrame MedAppGUI to control
	 * @param out 
	 */
	public MedAppController(MedAppGUI frame, PrintWriter out){
		con=null;
		this.out = out;
		this.frame = frame;
		login = new LoginGUI(this);
		loginSuccess = new LoginSuccessful(this);
		dP = new DoctorPanel(this);
		patientPanel = new PatientPanel(this);
		nursePanel = new NursePanel(this);
		saPanel = new SysAdminPanel(this);
		
		loginSuccessful = false;
		currentCard = "Login";
		currentUser = "";
		
		this.setLayout(cl);
		this.add("Login", this.login);
		this.add("LoginSuccess", this.loginSuccess);
		this.add("DoctorPanel", this.dP);
		this.add("PatientPanel",this.patientPanel);
		this.add("NursePanel",this.nursePanel);
		this.add("SysAdminPanel", this.saPanel);
	}
	
	/**
	 * Returns whether the login was a success or not
	 * @return boolean true if login was successful false if not
	 */
	public boolean getLoginSuccess(){
		return loginSuccessful;
	}
	
	/**
	 * After logging in, set the loginSuccess boolean value to true and false after logging out
	 * @param b true if logged in, false if logged out
	 */
	public void setLoginSuccess(boolean b){
		loginSuccessful = b;
	}
	
	/**
	 * Returns the current card of the dynamic panel
	 * @return String name of the card
	 */
	public String getCurrentCard(){
		return currentCard;
	}
	
	/**
	 * Sets the current card of the dynamic panel
	 * @param in String name of the card to change to
	 */
	public void setCurrentCard(String in){
		currentCard = in;
		cl.show(this, currentCard);
	}
	
	/**
	 * Returns the current logged in user
	 * @return username of the current user
	 */
	public String getCurrentUser(){
		return currentUser;
	}
	
	/**
	 * Sets the current user and their account type, clears fields if logging out
	 * @param in username of current user
	 * @param accountType accountType of current user
	 */
	public void setCurrentUser(String in, String accountType){
		currentUser = in;
		this.accountType=accountType;
		
		if(in.equals("")&&accountType.equals("")){
			login.clearFields();
		}
	}
	
	/**
	 * Sets the current user's account type, clears fields if logging out
	 * @param accountType accountType of current user
	 */
	public void setUserType(String accountType){
		this.accountType=accountType;
		
		if(accountType.equals("")){
			login.clearFields();
		}
	}
	
	/**
	 * Gets the current user's account type
	 */
	public String getUserType(){
		return this.accountType;
	}

	/**
	 * Makes the connection to the database and passes the Connection to all Panels of the program
	 * @return Connection to the database
	 */
	public Connection makeConnection(){
		con = null; 
		try { 
			Class.forName("com.mysql.jdbc.Driver").newInstance(); 
			con = DriverManager.getConnection("jdbc:mysql://localhost/velociraptorsattack", "attack", "velociraptors"); 
			if(!con.isClosed()) 
				System.out.println("Successfully connected to " + 
				"MySQL server using TCP/IP..."); 
		} catch(Exception e) { 
			out.println("Exception: " + e.getMessage()); 
		}
		
		String sql = "SELECT MAX(`DoctorsOrders_ID`) FROM `doctorsorders`";
		out.println(sql);
		try {
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			
			while(rs.next()){
				doctorsOrderID = rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String sql2 = "SELECT MAX(`T_ID`) FROM `treatmentrecord`";
		out.println(sql2);
		try {
			Statement stmt2 = con.createStatement();
			ResultSet rs2 = stmt2.executeQuery(sql2);
			
			while(rs2.next()){
				treatmentRecordID = rs2.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return con;
	}
	
	/**
	 * Sets the Logged in Doctor's info
	 * @param firstName String First Name of the Doctor
	 * @param lastName String Last Name of the Doctor
	 * @param SSN int SSN of the Doctor
	 */
	public void setDoctorInfo(String firstName, String lastName, int SSN){
		out.println("setDoctorInfo entererd");
		dP.setInfo(firstName, lastName, currentUser, SSN);
	}
	
	/**
	 * Sets the Logged in Nurse's info
	 * @param firstName String First Name of the Nurse
	 * @param lastName String Last Name of the Nurse
	 * @param SSN int SSN of the Nurse
	 */
	public void setNurseinfo(String firstName, String lastName, int SSN)
	{	
		out.println("setNurseInfo entererd");
		nursePanel.setInfo(firstName, lastName, currentUser, SSN);
	}

	/**
	 * Returns the Connection object con
	 * @return Connection con with connection the mysql database
	 */
	public Connection getCon() {
		out.println("getCon()");
		return con;
	}

	/**
	 * sets the patient table model in the doctor panel
	 */
	public void setDoctorPatientTableModel() {
		try {
			dP.tableInit();
			dP.tblDoctorPatientList.setModel(dP.myModel());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/**
	 * sets the patient table model in the nurse panel
	 */
	public void setNursePatientTableModel() {
		try {
			nursePanel.tableInit();
			nursePanel.tblNursePatientList.setModel(nursePanel.myModel());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	/**
	 * Sets the info of the current logged in patient
	 * @param firstName String First Name of the Patient
	 * @param lastName String Last Name of the Patient
	 * @param SSN int SSN of the Patient
	 */
	public void setPatientInfo(String firstName, String lastName, int SSN) {
		patientPanel.setInfo(firstName,lastName,currentUser,SSN);
		
	}
	
	/**
	 * Sets the info of the current logged in SysAdmin
	 * @param firstName String First Name of the SysAdmin
	 * @param lastName String Last Name of the SysAdmin
	 * @param ssn int SSN of the SysAdming
	 */
	public void setSysAdminInfo(String firstName, String lastName, int ssn){
		saPanel.setInfo(firstName, lastName, currentUser, ssn);
	}
	
	/**
	 * sets the user table model of the sysadmin panel
	 */
	public void setSysAdminPatientTableModel() {
		try {
			saPanel.tableInit();
			saPanel.tblDoctorPatientList.setModel(saPanel.myModel());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

//	public void reset(){
//		frame.exit();
//	}
	
	/**
	 * Returns the next incremented value of the doctor's order id based on the database
	 * @return int next doctor order iD
	 */
	public int getNextDoctorsOrderID(){
		doctorsOrderID++;
		out.println("Current Doctors Order ID: "+doctorsOrderID);
		return doctorsOrderID;
	}
	
	/**
	 * Returns the next incremented value of the treatment record id based on the database
	 * @return in next treatment record id
	 */
	public int getNextT_ID(){
		treatmentRecordID++;
		out.println("Current T_ID: "+treatmentRecordID);
		return treatmentRecordID;
	}

	/**
	 * @return the treatmentRecordID
	 */
	public int getTreatmentRecordID() {
		return treatmentRecordID;
	}

	/**
	 * @param treatmentRecordID the treatmentRecordID to set
	 */
	public void setTreatmentRecordID(int treatmentRecordID) {
		this.treatmentRecordID = treatmentRecordID;
	}

	/**
	 * @return the loginSuccessful
	 */
	public boolean isLoginSuccessful() {
		return loginSuccessful;
	}

	/**
	 * @param loginSuccessful the loginSuccessful to set
	 */
	public void setLoginSuccessful(boolean loginSuccessful) {
		this.loginSuccessful = loginSuccessful;
	}

	/**
	 * @return the accountType
	 */
	public String getAccountType() {
		return accountType;
	}

	/**
	 * @param accountType the accountType to set
	 */
	public void setAccountType(String accountType) {
		this.accountType = accountType;
	}
}
